package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

//@WebListener
public class 上下文容器操作监听 implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        ServletContext application = servletContextAttributeEvent.getServletContext();
        String key = servletContextAttributeEvent.getName();
        Object value = servletContextAttributeEvent.getValue();
        System.out.println("向application容器添加了"+value+",名字是:"+key);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        ServletContext application = servletContextAttributeEvent.getServletContext();
        String key = servletContextAttributeEvent.getName();
        Object value = servletContextAttributeEvent.getValue();
        System.out.println("将application容器删除了"+value+",名字是:"+key);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        ServletContext application = servletContextAttributeEvent.getServletContext();
        String key = servletContextAttributeEvent.getName();
        Object value = servletContextAttributeEvent.getValue();//修改前的值
        System.out.println("将容器application的内容："+value+",名字是:"+key+
                ",修改成："+application.getAttribute(key));
    }
}
